package richbar.com.richbar.view.pop;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.help.RecycleViewHelp;
import com.common.util.DisplayUtil;
import com.common.util.LogUtil;

import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.bean.Bean;
import richbar.com.richbar.bean.ShopResult;

/**
 * Created by QiYang on 2018/12/11.
 */

public class ViewTopListPop {
    private Activity context;

    public ViewTopListPop(Activity context) {
        this.context = context;
    }

    public void show(View v, List<ShopResult.DataEntity.HotCate> data, Callback callback){
        //准备PopupWindow的布局View
        View view = LayoutInflater.from(context).inflate(R.layout.pop_view_top, null);
        RecyclerView recyclerView = view.findViewById(R.id.recycleView);
        MyAdapter adapter = new MyAdapter(R.layout.item_text,data,callback);
        RecycleViewHelp.setManager(recyclerView,context);
        recyclerView.setAdapter(adapter);
        final PopupWindow popupWindow = new PopupWindow(
                DisplayUtil.dp2px(context,120), ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
        view.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);
        int popHeight = view.getMeasuredHeight();
        if (null != popupWindow && !popupWindow.isShowing()){
            LogUtil.e("PPAPAPAPPA","2");
            int[] location = new int[2];
            v.getLocationOnScreen(location);
//在控件上方显示    向上移动y轴是负数
            popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - DisplayUtil.dp2px(context,120) / 2, location[1]-popHeight);
        }
    }



    private void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1.0f) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }


    class MyAdapter extends BaseQuickAdapter<ShopResult.DataEntity.HotCate> {
        private Callback callback;
        private int length;

        public MyAdapter(int layoutResId, List<ShopResult.DataEntity.HotCate> data,Callback callback) {
            super(layoutResId, data);
            this.callback = callback;
            length = data.size();
        }

        @Override
        protected void convert(final BaseViewHolder holder, final ShopResult.DataEntity.HotCate bean) {
            holder.setText(R.id.tv,bean.getName());
            View line = holder.getView(R.id.line);
            if (holder.getAdapterPosition() == length - 1){
                line.setVisibility(View.GONE);
            }else {
                line.setVisibility(View.VISIBLE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callback.callback(bean);
                }
            });


        }
    }

    public interface Callback{
        void callback(ShopResult.DataEntity.HotCate bean);
    }
}
